home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / msdos / fractal / fdesi313 / fdes308s / fdesplot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-18  |  15.6 KB  |  518 lines

  1.  
  2. #include <float.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <graphics.h>
  6. #include <conio.h>
  7. #include <dos.h>
  8. #include "fdestria.h"
  9. #include "fdesign.h"
  10. #include "fdesequa.h"
  11. #include "fdesfile.h"
  12. #include "fdesmenu.h"
  13. #include "fdesmous.h"
  14. #include "fdeshres.h"
  15. #include "fdesvirt.h"
  16.  
  17. #define plot_small_left 420
  18. #define plot_small_bottom 140
  19. #define CONV_ITS 100            /* iterations required for convergence */
  20.  
  21. int pal_switch;
  22. int plot_type;                  /* types are:   0 big 16 color
  23.                                                 1 small 16 color
  24.                                                 2 big 256 color
  25.                                                 3 big 256 color hi-res
  26.                                                 4 virtual big 1000 x 1500
  27.                                                 5 FRACTINT ifs screen size
  28.                                                 6 16 color w/increment
  29.                                                 */
  30. int last_plot_type = 0;
  31. int IFS_changed;
  32. float x,y;
  33. float xscale,yscale,xoffset,yoffset,maxx,maxy;
  34. float a[MAXFUNC],b[MAXFUNC],c[MAXFUNC],d[MAXFUNC],e[MAXFUNC],f[MAXFUNC];
  35. float p[MAXFUNC];
  36. int n;
  37. /************************************************************************
  38.         PLOT SOME POINTS USING THE IFS CODES
  39. ************************************************************************/
  40. void plotabunch(int howmany)
  41. {
  42. int m,j,k;
  43. float pk,newx,newy;
  44.  
  45.         k = 0;
  46.     for (m=0; m<howmany; m++) {
  47.         pk = rand()/32767.0;
  48.         for (j=0; j<n; j++)
  49.         {
  50.             if (pk < p[j])
  51.             {
  52.                 k=j;
  53.                 break;
  54.             }
  55.         }
  56.                 newx = a[k] * x + b[k] * y + e[k];
  57.                 newy = c[k] * x + d[k] * y + f[k];
  58.                 x = newx;
  59.                 y = newy;
  60.                 putpixel((int)x,(int)y,colors[k%MAXCOLORS_]);
  61.     }
  62. }
  63. void plotabunch16(int howmany)
  64. {
  65. int m,j,k;
  66. float pk,newx,newy;
  67.  
  68.         k = 0;
  69.     for (m=0; m<howmany; m++) {
  70.         pk = rand()/32767.0;
  71.         for (j=0; j<n; j++)
  72.         {
  73.             if (pk < p[j])
  74.             {
  75.                 k=j;
  76.                 break;
  77.             }
  78.         }
  79.                 newx = a[k] * x + b[k] * y + e[k];
  80.                 newy = c[k] * x + d[k] * y + f[k];
  81.                 x = newx;
  82.                 y = newy;
  83.                 putpixel((int)x,(int)y,(getpixel((int)x,(int)y)+1)%maxcolor);
  84.     }
  85. }
  86. void plotabunchs(int howmany)
  87. {
  88. int m,j,k;
  89. float pk,newx,newy;
  90. int temp_x, temp_y;
  91.  
  92.         k = 0;
  93.     for (m=0; m<howmany; m++) {
  94.         pk = rand()/32767.0;
  95.         for (j=0; j<n; j++)
  96.         {
  97.             if (pk < p[j])
  98.             {
  99.                 k=j;
  100.                 break;
  101.             }
  102.         }
  103.                 newx = a[k] * x + b[k] * y + e[k];
  104.                 newy = c[k] * x + d[k] * y + f[k];
  105.                 x = newx;
  106.                 y = newy;
  107.                 temp_x = (int)x;
  108.                 temp_y = (int)y;
  109.                 if ((temp_x >= plot_small_left) && (temp_y < plot_small_bottom))
  110.                         putpixel(temp_x,temp_y,colors[k%MAXCOLORS_]);
  111.     }
  112. }
  113. /*
  114. void plotabunch_inc(int howmany)
  115. {
  116. int m,j,k;
  117. int oldpixel;
  118. float pk,newx,newy;
  119. float screenx, screeny;
  120.  
  121.         k = 0;
  122.     for (m=0; m<howmany; m++) {
  123.         pk = rand()/32767.0;
  124.         for (j=0; j<n; j++)
  125.         {
  126.             if (pk < p[j])
  127.             {
  128.                 k=j;
  129.                 break;
  130.             }
  131.         }
  132.                 newx = a[k] * x + b[k] * y + e[k];
  133.                 newy = c[k] * x + d[k] * y + f[k];
  134.                 x = newx;
  135.                 y = newy;
  136.                 screenx = x;
  137.                 screeny = y;
  138.         oldpixel = getpixel256(screenx,screeny);
  139.  
  140.                 if (oldpixel == 255) putpixel256(screenx, screeny, 1);
  141.                 else putpixel256(screenx, screeny, oldpixel+1);
  142.     }
  143. }
  144. */
  145. /*
  146.         virtual plot fractal plotter
  147. */
  148. void vplotabunch(int howmany)
  149. {
  150. int m,j,k;
  151. float pk,newx,newy;
  152.  
  153.         k = 0;
  154.     for (m=0; m<howmany; m++) {
  155.         pk = rand()/32767.0;
  156.         for (j=0; j<n; j++)
  157.         {
  158.             if (pk < p[j])
  159.             {
  160.                 k=j;
  161.                 break;
  162.             }
  163.         }
  164.                 newx = a[k] * x + b[k] * y + e[k];
  165.                 newy = c[k] * x + d[k] * y + f[k];
  166.                 x = newx;
  167.                 y = newy;
  168.                 vputpixel((int)x,(int)y);
  169.     }
  170. }
  171. /*
  172.         *** *** *** *** *** *** *** *** *** *** *** *** *** ***
  173.         Rescale IFS codes using xscale,xoffset,yscale,yoffset
  174.         *** *** *** *** *** *** *** *** *** *** *** *** *** ***
  175.         rel = 0         if relative to original triangles
  176.         rel = 1         if relative to already scaled triangles
  177. */
  178. triangle ttriangle0;                    /* re-scaled reference triangle */
  179. triangle ttriangles[MAXFUNC];           /* re-scaled triangle data stores */
  180. void IFS_rescale(float xscale, float xoffset, float yscale, float yoffset, int rel)
  181. {
  182. int i,j,k,m;
  183. float pt,pk,newx,newy;
  184.  
  185.         /* Now generate scaled copies of the triangles using the just
  186.            computed scale and offset factors */
  187.         if (rel == 0)
  188.         {
  189.                 for (i=0; i<3; i++)
  190.                 {
  191.                         ttriangle0.row[i] = triangle0.row[i]*yscale+yoffset;
  192.                         ttriangle0.col[i] = triangle0.col[i]*xscale+xoffset;
  193.                 }
  194.                 for (j=0; j<num_triangles; j++)
  195.                 {
  196.                         for (i=0; i<3; i++)
  197.                         {
  198.                                 ttriangles[j].row[i] = triangles[j].row[i]*yscale+yoffset;
  199.                                 ttriangles[j].col[i] = triangles[j].col[i]*xscale+xoffset;
  200.                         }
  201.                 }
  202.         } else
  203.         {
  204.                 for (i=0; i<3; i++)
  205.                 {
  206.                         ttriangle0.row[i] = ttriangle0.row[i]*yscale+yoffset;
  207.                         ttriangle0.col[i] = ttriangle0.col[i]*xscale+xoffset;
  208.                 }
  209.                 for (j=0; j<num_triangles; j++)
  210.                 {
  211.                         for (i=0; i<3; i++)
  212.                         {
  213.                                 ttriangles[j].row[i] = ttriangles[j].row[i]*yscale+yoffset;
  214.                                 ttriangles[j].col[i] = ttriangles[j].col[i]*xscale+xoffset;
  215.                         }
  216.                 }
  217.         }
  218.         /* Re-compute the IFS codes using the scaled triangles */
  219.         IFS_compute_all(num_triangles,ttriangles,&ttriangle0);
  220.         pt = 0;
  221.         for (i=0; i<num_triangles; i++)
  222.     {
  223.                 a[i] = IFS[1+(i*7)];
  224.                 b[i] = IFS[2+(i*7)];
  225.                 c[i] = IFS[3+(i*7)];
  226.                 d[i] = IFS[4+(i*7)];
  227.                 e[i] = IFS[5+(i*7)];
  228.                 f[i] = IFS[6+(i*7)];
  229.                 pt += IFS[7+(i*7)];
  230.         p[i] = pt;
  231.     }
  232.  
  233.     /* do iterations so (x,y) is on new attractor */
  234.         k = 0;
  235.         for (m=0; m<CONV_ITS; m++) {
  236.         pk = rand()/32767.0;
  237.         for (j=0; j<n; j++)
  238.         {
  239.             if (pk < p[j])
  240.             {
  241.                 k=j;
  242.                 break;
  243.             }
  244.         }
  245.                 newx = a[k] * x + b[k] * y + e[k];
  246.                 newy = c[k] * x + d[k] * y + f[k];
  247.                 x = newx;
  248.                 y = newy;
  249.     }
  250.  
  251. }
  252. /* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
  253.                 copy scaled triangles from ttriangle*
  254.                 to the input triangle array
  255.    *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
  256. void triangles_use_temp(void)
  257. {
  258. int i,j;
  259.         for (i=0; i<3; i++)
  260.         {
  261.                 triangle0.row[i] = ttriangle0.row[i];
  262.                 triangle0.col[i] = ttriangle0.col[i];
  263.         }
  264.         for (j=0; j<num_triangles; j++)
  265.         {
  266.                 for (i=0; i<3; i++)
  267.                 {
  268.                         triangles[j].row[i] = ttriangles[j].row[i];
  269.                         triangles[j].col[i] = ttriangles[j].col[i];
  270.                 }
  271.         }
  272.         IFS_changed = 1;
  273. }
  274.  
  275.  
  276. /*
  277.         Compute the limits of the IFS codes by executing several iterations
  278.         and storing the min and max values.
  279. */
  280. void plotlimits(unsigned int scale_its)
  281. {
  282. int m,j,k,i;
  283. float pk,newx,newy,pt;
  284. float xmin,xmax,ymin,ymax;
  285. float *frac;                            /* not necessary */
  286.  
  287.         area_scaled = 1.0;               /* zooming area scaling is 1.0 */
  288.         frac = IFS;
  289.     IFS_compute_all(num_triangles,triangles,&triangle0);
  290.  
  291.     n = frac[0];
  292.         pt = 0;
  293.     for (i=0; i<n; i++)
  294.     {
  295.         a[i] = frac[1+(i*7)];
  296.         b[i] = frac[2+(i*7)];
  297.         c[i] = frac[3+(i*7)];
  298.         d[i] = frac[4+(i*7)];
  299.         e[i] = frac[5+(i*7)];
  300.         f[i] = frac[6+(i*7)];
  301.         pt += frac[7+(i*7)];
  302.         p[i] = pt;
  303.     }
  304.     x = 0.0;
  305.     y = 0.0;
  306.  
  307.         k = 0;
  308.         for (m=0; m<scale_its; m++) {
  309.         if (m==CONV_ITS) {
  310.                     ymax = (ymin = y);
  311.                     xmax = (xmin = x); }
  312.         pk = rand()/32767.0;
  313.         for (j=0; j<n; j++)
  314.         {
  315.             if (pk < p[j])
  316.             {
  317.                 k=j;
  318.                 break;
  319.             }
  320.         }
  321.                 newx = a[k] * x + b[k] * y + e[k];
  322.                 newy = c[k] * x + d[k] * y + f[k];
  323.                 x = newx;
  324.                 y = newy;
  325.         if (m > CONV_ITS) {
  326.                         if (newx<xmin) xmin = newx;
  327.                         if (newx>xmax) xmax = newx;
  328.                         if (newy<ymin) ymin = newy;
  329.                         if (newy>ymax) ymax = newy; }
  330.     }
  331.     if ((plot_type == 0) || (plot_type == 2) || (plot_type == 3) || (plot_type == 6))
  332.         {
  333.                 /* set scaling here */
  334.                 if (xmax == xmin) xscale = 1;
  335.         else xscale = 0.95 * maxx / (xmax - xmin);
  336.                 if (ymax == ymin) yscale = 1;
  337.         else yscale = 0.95 * maxy / (ymax - ymin);
  338.                 if (xscale > yscale) xscale = yscale;
  339.                 else yscale = xscale;
  340.         xoffset = -((xmax+xmin)/2.0)*xscale + maxx/2.0;
  341.         yoffset = -((ymax+ymin)/2.0)*yscale + maxy/2.0;
  342.     }
  343.         else if (plot_type == 4)
  344.         {
  345.                 /* set scaling here */
  346.                 if (xmax == xmin) xscale = 1;
  347.         else xscale = 0.95 * VWIDTH / (xmax - xmin);
  348.                 if (ymax == ymin) yscale = 1;
  349.         else yscale = 0.95 * VHEIGHT / (ymax - ymin);
  350.                 if (xscale > yscale) xscale = yscale;
  351.                 else yscale = xscale;
  352.         xoffset = -((xmax+xmin)/2.0)*xscale + VWIDTH/2.0;
  353.         yoffset = -((ymax+ymin)/2.0)*yscale + VHEIGHT/2.0;
  354.         }
  355.         else if (plot_type == 1)
  356.         {
  357.                 /* set scaling for small plot in upper right corner */
  358.                 if (xmax == xmin) xscale = 1;
  359.         else xscale = 0.9 * (maxx - plot_small_left) / (xmax - xmin);
  360.                 if (ymax == ymin) yscale = 1;
  361.         else yscale = 0.9 * plot_small_bottom / (ymax - ymin);
  362.                 if (xscale > yscale) xscale = yscale;
  363.                 else yscale = xscale;
  364.         xoffset = -((xmax+xmin)/2.0)*xscale +
  365.             (plot_small_left + maxx)/2.0;
  366.         yoffset = -((ymax+ymin)/2.0)*yscale + plot_small_bottom/2.0;
  367.     }
  368.         else if (plot_type == 5)        /* FRACTINT screen size */
  369.         {
  370.                 /*
  371.                    Fractint's screen size is x=0 to x=16
  372.                    and y=-6 to y=6.
  373.                    approximately.
  374.                 */
  375.                 if (xmax == xmin) xscale = 1;
  376.         else xscale = 14.0 / (xmax - xmin);
  377.                 if (ymax == ymin) yscale = 1;
  378.                 else yscale = 10.0 / (ymax - ymin);
  379.                 if (xscale > yscale) xscale = yscale;
  380.                 else yscale = xscale;
  381.                 yscale = -yscale;
  382.                 xoffset = -(((xmin+xmax)/2.0)*xscale);
  383.                 yoffset = -(((ymin+ymax)/2.0)*yscale) + 5.0;
  384.         }
  385.  
  386.         /* scale relative to originally input triangles */
  387.         IFS_rescale(xscale,xoffset,yscale,yoffset,0);
  388.  
  389.         last_plot_type = plot_type;
  390. }
  391.  
  392. /************************************************************************
  393.         BIG PLOT: PLOT FRACTAL UNTIL MOUSE CLICK
  394.         SMALL PLOT: PLOT A SMALL # OF POINTS
  395. ************************************************************************/
  396. int doIFSrand(void)   /* plot_type are commented at top of this file */
  397. {
  398. int rcode;
  399. long count;
  400. mouse_state m;
  401. char sbuf[80];
  402.  
  403.         /* re-compute limits if new IFS or plot type changed */
  404.         if (IFS_changed || (last_plot_type != plot_type)) {
  405.                 if (plot_type == 1) {
  406.                         setfillstyle(SOLID_FILL,BLACK);
  407.                         bar(plot_small_left,0,maxx,plot_small_bottom);
  408.                         plotlimits(300);
  409.                 }
  410.                 else if ((plot_type == 0) || (plot_type == 6))
  411.                 {
  412.             putmsg(150,175,"Wait...Computing Plot limits",DARKGRAY,LIGHTCYAN);
  413.                         plotlimits(3000);
  414.                         clrmsg();
  415.                 }
  416.                 else if ((plot_type == 2) || (plot_type == 3))
  417.                 {
  418. /*
  419.             putmsg(150,175,"Wait...Computing Plot limits",0,128);
  420. */
  421.             plotlimits(3000);
  422. /*
  423.             clrmsg();
  424. */
  425.         }
  426.                 else if (plot_type == 4)
  427.                 {
  428.             putmsg(150,175,"Scaling Main Display",DARKGRAY,LIGHTCYAN);
  429.                         delay(1000);
  430.                         clrmsg();
  431.                 }
  432.         IFS_changed = 0;
  433.         }
  434.         if (plot_type == 1)
  435.         {
  436.                 plotabunchs(10);                /* small plot plots 10 then */
  437.                                                 /* leaves. */
  438.                 return(0);
  439.         }
  440.         else if (plot_type == 0)
  441.         {
  442.                 do
  443.                 {
  444.                         plotabunch(500);        /* plots 500 points */
  445.                         rcode = mouse_press(&m);
  446.                 } while ((!rcode) && (!kbhit()));
  447.                 if (kbhit()) getch();
  448.                 return(rcode);
  449.         }
  450.     else if (plot_type == 6)
  451.         {
  452.                 do
  453.                 {
  454.                         plotabunch16(500);        /* plots 500 points */
  455.                         rcode = mouse_press(&m);
  456.                 } while ((!rcode) && (!kbhit()));
  457.                 if (kbhit()) getch();
  458.                 return(rcode);
  459.         }
  460.         else if (plot_type == 4)                /* virtual screen plot */
  461.         {
  462.                 putmsg_d(200,200,"          Virtual Plot Status         ",GREEN,WHITE);
  463.                 setfillstyle(SOLID_FILL,GREEN);
  464.                 bar(200,75,500,190);
  465.  
  466.                 count = 0;
  467.                 do
  468.                 {
  469.                         vplotabunch(5000);        /* plots 1500 points */
  470.                         count += 5000;
  471.  
  472.                         sprintf(sbuf,"Count = %ld",count);
  473.                         putmsg_d(200,50,sbuf,BLUE,WHITE);
  474.  
  475.                         setfillstyle(SOLID_FILL,RED);
  476.                         bar(200,75,min((300.0*count)/virt_target_size,300.0)+200,190);
  477.  
  478.                         rcode = mouse_press(&m);
  479.  
  480.                 } while ((!rcode) && (!kbhit()) && (count < virt_target_size));
  481.                 if (count >= virt_target_size)
  482.                         putmsg_d(290,130,"Plot Complete",RED,WHITE);
  483.                 if (kbhit()) getch();
  484.                 return(rcode);
  485.     }
  486. /*
  487.         else if ((plot_type == 2) || (plot_type == 3))
  488.         {
  489.                 pixel_max = 0;
  490.                 pal_switch = -2;                /* initial quantization */
  491.                 set_palette(pal_switch);
  492.                 do
  493.                 {
  494.                         plotabunch_inc(500);            /* plots 500 points */
  495.             if (-pixel_max < pal_switch)
  496.                         {
  497.                 if (pal_switch > -256)
  498.                                 {
  499.                     pal_switch -= 2;
  500.                                         set_palette(pal_switch);
  501.                                 }
  502.                         }
  503.                         mouse_get(&m);
  504.                         if (color_shift != m.row)
  505.                         {
  506.                                 color_shift = m.row;
  507.                 set_palette(pal_switch);
  508.                         };
  509.                         rcode = mouse_press(&m);
  510.                 } while ((!rcode) && (!kbhit()));
  511.                 if (kbhit()) getch();
  512.                 return(rcode);
  513.     }
  514. */
  515.     return(0);
  516. }
  517.  
  518.